<?php

class imagem
{

    static function upload($nome_img,$dir,$id,$tumb=NULL,$width = 200, $height = 200)
    {
        $img = $_FILES[$nome_img];
        $imageAdapter = new Zend_File_Transfer_Adapter_Http();
        $imageAdapter->setDestination($dir);
        $filenew = '';
        $ext = '';
        if(is_uploaded_file($img['tmp_name'])){

            foreach (glob($dir.$id.'.*') as $filename) unlink($filename);

            $ext = end(explode('.', $img['name']));

            $filenew = $dir.$id.'.'.$ext;

            $imageAdapter->addFilter('Rename', array(
                    'target' => $filenew,
                    'overwrite' => true
            ));

            if (!$imageAdapter->receive($nome_img)){ //A Imagem Não Foi Recebida Corretamente
                $messages = $imageAdapter->getMessages[$nome_img];
                
            }else{ //Arquivo Enviado Com Sucesso
                $filename = $imageAdapter->getFileName($nome_img);
                chmod($filename, 0777);
                
                if($tumb == TRUE){
                    
                    foreach (glob($dir.$id.'_tb.*') as $filenewp) unlink($filenewp);
                    
                    Zend_Loader::loadClass('WideImage','library/wideimage/lib');
                    
                    $image = WideImage::load($filename);
                    
                    //$resized = $image->resize($width, $height, 'inside'); 
                    //$resized = $image->crop('0', '0' , $width, $height);
                    $resized = $image->resize($width, $height, 'outside')->crop('50% - '.($width/2), '50% - '.($height/2), $width, $height);
                      
                    $filenewp = $dir.$id.'_tb.'.end(explode('.', $filename));
                    
                    $resized->saveToFile($filenewp);
                    chmod($filenewp, 0777);
                }
            }
        }
        return array('filenew'=>$filenew,'ext'=>$ext);
    }
    
    static function rename($id,$dir,$file,$tumb=NULL)
    {
        $filenew = $dir.$id.'.'.end(explode('.', $file));
        rename($file,$filenew);
        if($tumb == TRUE){
            $filenewp = $dir.$id.'_tb.'.end(explode('.', $file));
            $file = str_replace('.', '_tb.', $file);
            rename($file,$filenewp);
        }
        
        /*    
        $img = $dir.$id.'.*';
        foreach (glob($img) as $src) {
            $filenew = $dir.$id.'.'.end(explode('.', $src));
            rename($src,$filenew);
        }
         * *
         */
    }
    static function delete($id,$dir,$tumb=NULL)
    {
        foreach (glob($dir.$id.'.*') as $file) { 
            unlink($file);
            if($tumb == TRUE) unlink(str_replace('.', '_tb.', $file));
        }
    }
    
    static function formatNomeArquivo($id,$datahora) {
        $str =  md5($id.$datahora);
        return $str;
    }
    
    static function reize($filename, $filenewp, $width = 200, $height = 200)
    {
           
        Zend_Loader::loadClass('WideImage','library/wideimage/lib');

        $image = WideImage::load($filename);
        
        //$resized = $image->resize($width, $height, 'inside'); 
        //$resized = $image->crop('50% - '.($width/2), '50% - '.($height/2) , $width, $height);
        
        $resized = $image->resize($width, $height,'outside')->crop('50% - '.($width/2), '50% - '.($height/2), $width, $height);
                    
        $resized->saveToFile($filenewp);
        
    }
    
    static function reizeFull($filename, $filenewp, $width = 200, $height = 200, $fit = 'outside')
    {
           
        Zend_Loader::loadClass('WideImage','library/wideimage/lib');

        $image = WideImage::load($filename);

        //$resized = $image->resize($width, $height, $fit);
        
        $resized = $image->resize($width, $height, $fit)->crop('50% - '.($width/2), '50% - '.($height/2), $width, $height);
        
        $resized->saveToFile($filenewp);
        
    }
    
    static function reizeWH($filename, $filenewp, $width = 200, $height = 200)
    {
           
        Zend_Loader::loadClass('WideImage','library/wideimage/lib');

        $image = WideImage::load($filename);
        
        $resized = $image->resize($width, $height, 'outside'); 
                 
        $resized->saveToFile($filenewp);
        
    }
    
}

